@page "/fetchdata"
@page "/fetchdata/{StartDate:datetime}"
@inject WeatherForecastService ForecastService

<h1 id="fetch-data">Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>

@if (forecasts == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class='table'>
        <thead>
            <tr>
                <th>Date</th>
                <th>Temp. (C)</th>
                <th>Temp. (F)</th>
                <th>Summary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in forecasts)
            {
                <tr>
                    <td>@forecast.DateFormatted</td>
                    <td>@forecast.TemperatureC</td>
                    <td>@forecast.TemperatureF</td>
                    <td>@forecast.Summary</td>
                </tr>
            }
        </tbody>
    </table>
    <p>
        <a href="fetchdata/@startDate.AddDays(-5).ToString("yyyy-MM-dd")" class="btn btn-secondary float-left">
            ◀ Previous
        </a>
        <a href="fetchdata/@startDate.AddDays(5).ToString("yyyy-MM-dd")" class="btn btn-secondary float-right">
            Next ▶
        </a>
    </p>
}

@code {
    [Parameter] public DateTime? StartDate { get; set; }

    WeatherForecast[] forecasts;
    DateTime startDate;

    protected override async Task OnParametersSetAsync()
    {
        startDate = StartDate.GetValueOrDefault(DateTime.Now);
        forecasts = await ForecastService.GetForecastAsync(startDate);
    }
}
